enum ControllerBtn 
{
    NONE,
    LEFT,
    RIGHT,
    UP,
    DOWN,
    A,
    B,
    SELECT,
    START,
}
enum ControllerMode
{
    JOY,
    KEYBOARD,
    JOYSIMKEYBOARD,
}
class Controller
{
    constructor(div: HTMLDivElement)
    {
        let imgs = div.getElementsByTagName("img");
        for (var i = 0; i < imgs.length; i++)
        {
            let key: ControllerBtn = ControllerBtn.NONE;

            let img = imgs[i];
            if (img.name == "left")
                key = ControllerBtn.LEFT;
            else if (img.name == "right")
                key = ControllerBtn.RIGHT;
            else if (img.name == "up")
                key = ControllerBtn.UP;
            else if (img.name == "down")
                key = ControllerBtn.DOWN;
            else if (img.name == "a")
                key = ControllerBtn.A;
            else if (img.name == "b")
                key = ControllerBtn.B;
            else if (img.name == "select")
                key = ControllerBtn.SELECT;
            else if (img.name == "start")
                key = ControllerBtn.START;

            if (key != ControllerBtn.NONE)
            {
                this.mapBtnImg[key] = img;
                img.onmousedown = (e) =>
                {
                    this.dokeydown(key);
                };
                img.onmouseup = (e) =>
                {
                    this.dokeyup(key);
                };
                img.onmouseleave = (e) =>
                {
                    this.dokeyup(key);
                }
                img.ontouchstart = (e) =>
                {
                    this.dokeydown(key);
                };
                img.ontouchend = (e) =>
                {
                    this.dokeyup(key);
                };
                img.ontouchcancel = (e) =>
                {
                    this.dokeyup(key);
                };
                img.ontouchmove = (e) =>
                {
                    var t = e.changedTouches[0];
                    if (t.clientX < img.x || t.clientY < img.y || t.clientX > (img.x + img.width) || t.clientY > (img.y + img.height))
                        this.dokeyup(key);

                }
            }
        }
        this.mapBtnState[ControllerBtn.LEFT] = false;
        this.mapBtnState[ControllerBtn.RIGHT] = false;
        this.mapBtnState[ControllerBtn.UP] = false;
        this.mapBtnState[ControllerBtn.DOWN] = false;
        this.mapBtnState[ControllerBtn.A] = false;
        this.mapBtnState[ControllerBtn.B] = false;
        this.mapBtnState[ControllerBtn.SELECT] = false;
        this.mapBtnState[ControllerBtn.START] = false;
        document.addEventListener("keydown", (ev: KeyboardEvent) =>
        {
            let key = this.convertKeyCode(ev.keyCode);
            if (key == ControllerBtn.NONE)
                return;
            this.dokeydown(key);

        });
        document.addEventListener("keyup", (ev: KeyboardEvent) =>
        {
            let key = this.convertKeyCode(ev.keyCode);
            if (key == ControllerBtn.NONE)
                return;
            this.dokeyup(key);

        });

    }
    private dokeydown(key: ControllerBtn)
    {
        this.mapBtnState[key] = true;
        this.onJoyBtnDown(key);
        if (key == ControllerBtn.SELECT || key == ControllerBtn.START)
        {
            this.mapBtnImg[key].src = "img/b_long_s.png";
        }
        else
        {
            this.mapBtnImg[key].src = "img/b_s.png";
        }
    }
    private dokeyup(key: ControllerBtn)
    {
        this.mapBtnState[key] = false;
        this.onJoyBtnUp(key);
        if (key == ControllerBtn.SELECT || key == ControllerBtn.START)
        {
            this.mapBtnImg[key].src = "img/b_long.png";
        }
        else
        {
            this.mapBtnImg[key].src = "img/b.png";
        }
    }

    convertKeyCode(keycode: number): ControllerBtn
    {
        let key: ControllerBtn = ControllerBtn.NONE;
        if (keycode == 87)//w
            key = ControllerBtn.UP;
        if (keycode == 83)//s
            key = ControllerBtn.DOWN;
        if (keycode == 65)//a
            key = ControllerBtn.LEFT;
        if (keycode == 68)//d
            key = ControllerBtn.RIGHT;
        if (keycode == 32)//space
            key = ControllerBtn.SELECT;
        if (keycode == 13)//enter
            key = ControllerBtn.START;
        if (keycode == 75)//k
            key = ControllerBtn.A;
        if (keycode == 74)//j
            key = ControllerBtn.B;
        return key;
    }
    mapBtnImg: { [id: number]: HTMLImageElement } = {};
    mapBtnState: { [id: number]: boolean } = {};
    mode: ControllerMode = ControllerMode.JOY;
    onJoyBtnDown: (key: ControllerBtn) => void = () => { };
    onJoyBtnUp: (key: ControllerBtn) => void = () => { };
}
